<?php
class Admin_NewsController extends Zend_Controller_Action {
	public function preDispatch() {
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			if ($this->_request->getActionName () != "login") {
				$this->_redirect ( "/admin/index/login" );
			}
		}
	}
	public function init() {
		/* Initialize action controller here */
		$auth = Zend_Auth::getInstance ();
		$infoUser = $auth->getIdentity ();
		$this->view->fullName = $infoUser->USERNAME;
		$this->_model = new Default_Model_DbTable_News ();
		Zend_Layout::startMvc ( array (
				"layoutPath" => APPLICATION_PATH . "/layouts/scripts",
				"layout" => "admin" 
		) );
	}
	public function indexAction() {
		// action body
		$status = $this->getRequest ()->getParam ( "sorting" );
		$mainMenu = $this->getRequest ()->getParam ( "sortIntro" );
		
		$this->view->headTitle ( "Quản lý tin tức", "PREPEND" );
		$this->view->title = "Quản lý tin tức";
		
		if ($mainMenu == 0) {
			if ($status != null)
				$news = $this->_model->getNewsByStatus ( $status );
			else
				$news = $this->_model->getAllNewsAdmin ();
		} else {
			if ($mainMenu == 0 && $status == '-1') {
				$news = $this->_model->getAllNewsAdmin ();
			} elseif ($status != '-1' && $mainMenu == 0)
				$news = $this->_model->getNewsByStatus ( $status );
			elseif ($mainMenu != 0)
				$news = $this->_model->getNewsByCategoryIdAdmin ( $mainMenu );
		}
		
		$paginator = Zend_Paginator::factory ( $news );
		$paginator->setItemCountPerPage ( 9 );
		$paginator->setPageRange ( 5 );
		$currentPage = $this->_request->getParam ( "page", 1 );
		$paginator->setCurrentPageNumber ( $currentPage );
		$this->view->allNews = $paginator;
	}
	public function addAction() {
		$cat = new Default_Model_DbTable_Category ();
		$this->view->allCat = $cat->getAllCategory ();
		$auth = Zend_Auth::getInstance ();
		$infoUser = $auth->getIdentity ();
		$upload = new Private_Upload ();
		if ($this->getRequest ()->isPost ()) {
			$aID = $infoUser->ID;
			$Title = $this->_getParam ( "Title" );
			$Cat = $this->_getParam ( "Cat" );
			$Desc = $this->_getParam ( "Desc" );
			$Content = $this->_getParam ( "Content" );
			$Tags = $this->_getParam ( "Tags" );
			$update = date ( "Y-m-d h:m" );
			$Stt = $this->_getParam ( "Stt" );
			
			if ($_FILES ['PImg'] ['name'] == "" && $this->_request->getParam ( "textImg" ) == "") {
				
				$img = "/ishali/public/uploaded/images/no_image.jpg";
				$this->_model->addNews ( $Cat, $aID, $Title, $update, $Desc, $Content, $Tags, $img, $Stt );
				
				$this->_redirect ( "/admin/news" );
			} elseif ($this->_request->getParam ( "textImg" )) {
				// loi tai day
				
				$img = $this->_request->getParam ( "textImg" );
				
				$this->_model->addNews ( $Cat, $aID, $Title, $update, $Desc, $Content, $Tags, $img, $Stt );
				
				$this->_redirect ( "/admin/news" );
			} else {
				
				$img = "/ishali/public/uploaded/images/news/" . $_FILES ['PImg'] ["name"];
				$upload->upload ( "news" );
				$this->_model->addNews ( $Cat, $aID, $Title, $update, $Desc, $Content, $Tags, $img, $Stt );
				$this->_redirect ( "/admin/news" );
			}
		}
	}
	public function editAction() {
		$cat = new Default_Model_DbTable_Category ();
		$this->view->allCat = $cat->getAllCategory ();
		$auth = Zend_Auth::getInstance ();
		$infoUser = $auth->getIdentity ();
		$upload = new Private_Upload ();
		$id = $this->_getParam ( "id" );
		$news = $this->_model->getNewsById ( $id );
		$this->view->News = $news;
		if ($this->getRequest ()->isPost ()) {
			$aID = $infoUser->ID;
			$Title = $this->_getParam ( "Title" );
			$Cat = $this->_getParam ( "Cat" );
			$Desc = $this->_getParam ( "Desc" );
			$Content = $this->_getParam ( "Content" );
			$Tags = $this->_getParam ( "Tags" );
			$update = date ( "Y-m-d" );
			$Stt = $this->_getParam ( "Stt" );
			if ($_FILES ['PImg'] ['name'] == null && $this->_request->getParam ( "textImg" ) == "") {
				$img = "/ishali/public/uploaded/images/no_image.jpg";
				$this->_model->updateNews ( $id, $Cat, $aID, $Title, $update, $Desc, $Content, $Tags, $img, $Stt );
				$this->_redirect ( "/admin/news" );
			} elseif ($this->_request->getParam ( "textImg" ) != null && $_FILES ['PImg'] ['name'] == null) {
				$img = $this->_request->getParam ( "textImg" );
				$this->_model->updateNews ( $id, $Cat, $aID, $Title, $update, $Desc, $Content, $Tags, $img, $Stt );
				$this->_redirect ( "/admin/news" );
			} else {
				$img = "/ishali/public/uploaded/images/news/" . $_FILES ['PImg'] ["name"];
				$upload->upload ( "news" );
				$this->_model->updateNews ( $id, $Cat, $aID, $Title, $update, $Desc, $Content, $Tags, $img, $Stt );
				$this->_redirect ( "/admin/news" );
			}
		}
	}
	public function delAction() {
		$this->_model->removeNews ( $this->_getParam ( "id" ) );
		$this->_redirect ( "admin/news" );
	}
	public function activeAction() {
		$id = $this->_getParam ( "id" );
		$this->_model->updateStt ( $id );
		$this->_redirect ( "/admin/news" );
	}
}

